import {mapActions, mapGetters, mapMutations} from 'vuex';
import moment from 'moment-timezone';

moment.tz.setDefault("Asia/Shanghai");

const CommonMixin = {
    methods:{
        /**
         * 设置时间组件可选范围
         */
        __disabledDateBeforeCurrent(current) {
            // Can not select days before today and today
            return current && current < moment().endOf('day');
        },
    
        /**
         * moment实例
         * @param args
         * @returns {*|moment.Moment}
         * @private
         */
        __momentInstance(...args) {
            return moment(...args);
        },
    
        /**
         * 路由参数
         * @param key
         * @returns {*}
         * @private
         */
        __getParam(key) {
            return this.$route.params[key];
        },
    
        /**
         * 路由query
         * @param key
         * @returns {*}
         * @private
         */
        __getQuery(key) {
            return this.$route.query[key];
        },
    
        /**
         *  异常提示
         * @param content
         * @param title
         * @private
         */
        __alertResponseException(content, title = '错误提示') {
            this.$notification.error({
                message: title,
                description: content,
                duration: 4
            });
        },
    
        /**
         * 跳转
         * @param route
         * @private
         */
        __redirectTo (route) {
            return this.$router.push(route);
        },
    },
};

const GroupMixin = {
    
    data () {
        return {
            e$group: {
                routers: {
                    index: 'api/group',
                    detail: id => { return `${this.e$group.routers.index}/${id}`; },
                    students: id => { return `${this.e$group.routers.detail(id)}/student`; },
                    studentsJoin: (id, studentID = false) => {
                        return `${this.e$group.routers.detail(id)}/join${studentID === false ? '' : '/' + studentID}`;
                    },
                    studentsDrop: (id, studentID = false) => {
                        return `${this.e$group.routers.detail(id)}/drop${studentID === false ? '' : '/' + studentID}`;
                    },
                },
            },
        };
    },
    
};

const VideoMixin = {
    
    data () {
        return {
            e$video: {
                routers: {
                    index: 'api/video',
                    detail: video => { return `${this.e$video.routers.index}/${video}`; },
                    play: video => {return `${this.e$video.routers.detail(video)}/play`;},
                    createAuth: () => {return `${this.e$video.routers.index}/auth/create`},
                    refreshAuth: () => {return `${this.e$video.routers.index}/auth/refresh`},
                    viewSync: (viewRecordID) => {
                        return `api/view/record/${viewRecordID}`;
                    },
                },
                /**
                 * 创建上传凭证
                 * @param fileinfo
                 * @returns {*}
                 * @private
                 */
                __createVideoUploadAuth: (fileinfo) => {
                    return this.$http.post(this.e$video.routers.createAuth(), fileinfo);
                },
        
                /**
                 * 刷新上传凭证
                 * @param fileinfo
                 * @returns {*}
                 * @private
                 */
                __updateVideoUploadAuth: (fileinfo) => {
                    return this.$http.post(this.e$video.routers.refreshAuth(), fileinfo);
                },
    
                /**
                 * 创建播放凭证
                 * @param id
                 * @returns {*}
                 * @private
                 */
                __createVideoPlayerAuth: (id) => {
                    return this.$http.get(this.e$video.routers.play(id));
                },
    
                /**
                 * 同步观看记录
                 * @param viewRecordID
                 * @param data
                 * @returns {*}
                 * @private
                 */
                __studentViewRecordSync: (viewRecordID, data) => {
                    return this.$http.post(this.e$video.routers.viewSync(viewRecordID), data);
                },
            },
        };
    },
    
};

const UserMixin = {
    
    data () {
        return {
            e$user: {
                routers: {
                    profile: 'api/profile/update',
                },
    
                /**
                 * 更新用户信息
                 * @param data
                 * @returns {*}
                 * @private
                 */
                __updateCurrentProfile: (data) => {
                    return this.$http.post(this.e$user.routers.profile, data);
                },
            },
        };
    },
    
    methods: {
        ...mapActions(['Logout', 'GetInfo']),
        ...mapMutations(['SET_AVATAR', 'SET_NAME', 'SET_EMAIL', 'SET_ROLES', 'SET_PHONE']),
        ...mapGetters(['nickname', 'avatar', 'roles', 'email', 'phone']),
    },
    
};

const EduMixin = {
    
    data () {
        return {
            e$course: {
                routers: {
                    index: 'api/course',
                    detail: (id) => { return `${this.e$course.routers.index}/${id}`; },
                    periods: e$course => { return `${this.e$course.routers.index}/${e$course}/e$period`; },
                },
                //  查询参数
                queryParams: {like: '', mode: '', type: '',},
                //  课程类型参数
                types: [],
                //  课程模式参数
                mode: [],
                //  table列参数
                columns: [
                    {
                        title: '课程名称',
                        dataIndex: 'LABEL',
                        scopedSlots: {customRender: 'label'},
                        // fixed: 'left',
                        width: '100px',
                        align: 'left',
                
                    },
                    {
                        title: '所属类型',
                        dataIndex: 'TYPE',
                        scopedSlots: {customRender: 'types'},
                        width: '20%',
                        align: 'left',
                    },
                    {
                        title: '课程模式',
                        dataIndex: 'MODE',
                        scopedSlots: {customRender: 'mode'},
                        width: '12%',
                        align: 'left',
                    },
                    {
                        title: '创建人',
                        dataIndex: 'created',
                        scopedSlots: {customRender: 'member'},
                        width: '8%',
                        align: 'center',
                    },
                    {
                        title: '更新时间',
                        dataIndex: 'updated_at',
                        align: 'center',
                        width: '16%',
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        // fixed: 'right',
                        scopedSlots: {customRender: 'action'},
                        width: '10%',
                        align: 'right',
                    }
                ],
                //  操作参数
                operate: {
                    title: '添加课程',
                    visible: false,
                    data: {},
                    formItemLayout: {
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                    },
                    form: this.$form.createForm(this),
                    type: 'create',
                    submitBtn: {
                        loading: false,
                        disabled: false
                    },
                },
                //
                lists: false,
                loadCourseList: parameter => {
                    if( this.e$course.lists === false ) {
                        this.$http.get(this.e$course.routers.index, {params: parameter}).then(res => {
                            this.e$course.lists = res.data;
                        });
                    }
                    return this.e$course.lists;
                },
            },
            e$period: {
                routers: {
                    index: 'api/period',
                    detail: (id) => { return `${this.e$period.routers.index}/${id}`; },
                    sections: (id) => { return `${this.e$period.routers.detail(id)}/section`; },
                    exams: (id) => { return `${this.e$period.routers.detail(id)}/exam`; },
                    batchStore: (id) => { return `${this.e$period.routers.detail(id)}/batchStore`; },
                    publish: (id) => { return `${this.e$period.routers.detail(id)}/publish`; },
                    students: (id) => { return `${this.e$period.routers.detail(id)}/student`; },
                    studentsJoin: (id, group = false) => {
                        return group ? `${this.e$period.routers.students(id)}/join/${group}` : `${this.e$period.routers.students(id)}/join`;
                    },
                    studentsDrop: (id, group = false) => {
                        return group ? `${this.e$period.routers.students(id)}/drop/${group}` : `${this.e$period.routers.students(id)}/drop`;
                    },
                },
                //  table参数
                columns: [
                    {
                        title: '课期名称',
                        dataIndex: 'LABEL',
                        scopedSlots: {customRender: 'label'},
                        width: '20%',
                        align: 'left',
                    },
                    {
                        title: '状态',
                        dataIndex: 'PUBLISHED_AT',
                        scopedSlots: {customRender: 'published'},
                        align: 'center',
                        width: '16%',
                    },
                    {
                        title: '更新时间',
                        dataIndex: 'updated_at',
                        align: 'center',
                        width: '16%',
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        fixed: 'right',
                        scopedSlots: {customRender: 'action'},
                        width: '10%',
                        align: 'right',
                    }
                ],
                //  操作参数
                operate: {
                    title: '添加课期',
                    visible: false,
                    data: {},
                    formItemLayout: {
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                    },
                    form: this.$form.createForm(this),
                    type: 'create',
                    submitBtn: {
                        loading: false,
                        disabled: false
                    },
                    course: {}
                },
                //  详情操作参数
                detailOperate: {
                    visible: false,
                    title: '',
                    course: {},
                    period: {},
                    sections: [],
                },
                //  批量排课
                batch: {
                    visible: false,
                    form: this.$form.createForm(this),
                    data: {},
                    submitBtn: {
                        loading: false,
                        disabled: false
                    },
                },
            },
            e$section: {
                routers: {
                    index: 'api/section',
                    detail: (id) => { return `${this.e$section.routers.index}/${id}`; },
                    update: (id) => { return this.e$section.routers.detail(id); },
                    destroy: (id) => { return this.e$section.routers.detail(id); },
                    status: (id, status) => { return `${this.e$section.routers.detail(id)}/status/${status}`; },
                    video: (id) => { return `${this.e$section.routers.detail(id)}/video`; },
                    homework: (id) => {return `${this.e$section.routers.detail(id)}/homework`;},
                },
                columns: [
                    {
                        title: '课时',
                        dataIndex: 'LABEL',
                        scopedSlots: {customRender: 'label'},
                        width: '10%',
                        // fixed: 'left',
                        align: 'left',
                    },
                    {
                        title: '教员',
                        dataIndex: 'teacher',
                        scopedSlots: {customRender: 'teacher'},
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '资源',
                        dataIndex: 'resource',
                        scopedSlots: {customRender: 'resource'},
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '上课时间',
                        dataIndex: 'start_at_diff',
                        scopedSlots: {customRender: 'start'},
                        align: 'center',
                        width: '20%',
                    },
                    {
                        title: '来源',
                        dataIndex: 'FROM',
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '状态',
                        dataIndex: 'STATUS',
                        scopedSlots: {customRender: 'status'},
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '视频',
                        dataIndex: 'VIDEO',
                        scopedSlots: {customRender: 'video'},
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '作业',
                        dataIndex: 'HOMEWORK',
                        scopedSlots: {customRender: 'homework'},
                        align: 'center',
                        width: '10%',
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        fixed: 'right',
                        scopedSlots: {customRender: 'action'},
                        width: '10%',
                        align: 'right',
                    }
                ],
                selectedRowKeys: [],
                selectedRows: [],
        
                // custom table alert & rowSelection
                options: {
                    alert: {
                        show: true, clear: () => {
                            this.selectedRowKeys = [];
                            this.selectedRows = []
                        }
                    },
                    rowSelection: {
                        selectedRowKeys: this.selectedRowKeys,
                        onChange: this.onSelectChange
                    }
                },
                optionAlertShow: true,
                loadData: parameter => {
                    return this.$http.get(
                        this.e$period.routers.sections(this.e$period.detailOperate.e$period.ID), Object.assign(parameter, {})
                    ).then(res => {
                        return {
                            pageNo: parameter.pageNo,
                            pageSize: parameter.pageSize,
                            totalCount: res.count,
                            totalPage: res.count / parameter.pageSize,
                            data: res.data,
                        }
                    })
                }
            },
            e$student: {
                loadData: parameter => {
                    return this.$http.get(
                        this.e$period.routers.students(this.e$period.detailOperate.e$period.ID), Object.assign(parameter, {})
                    ).then(res => {
                        return res;
                    })
                },
                lists: [],
                columns: [
                    {
                        title: '课期名称',
                        dataIndex: 'LABEL',
                        scopedSlots: {customRender: 'label'},
                        width: '20%',
                        align: 'left',
                    },
                    {
                        title: '状态',
                        dataIndex: 'PUBLISHED_AT',
                        scopedSlots: {customRender: 'published'},
                        align: 'center',
                        width: '16%',
                    },
                    {
                        title: '更新时间',
                        dataIndex: 'updated_at',
                        align: 'center',
                        width: '16%',
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        fixed: 'right',
                        scopedSlots: {customRender: 'action'},
                        width: '10%',
                        align: 'right',
                    }
                ],
                showJoin: false,
                loading: false,
            },
            e$group: {
                routers: {
                    index: 'api/group',
                    join: (group = null) => {
                    },
                },
            },
            e$exam: {
                loading: false,
                list: [],
                operate: {
                    visible: false,
                    form: this.$form.createForm(this),
                    data: {},
                    submitBtn: {
                        loading: false,
                        disabled: false
                    },
                    title: '创建考试',
                    type: 'create',
                }
            },
            e$teacher: {
                routers: {
                    index: 'api/teacher',
                    detail: id => { return `${this.e$teacher.routers.index}/${id}`; },
                    periods: id => { return `${this.e$teacher.routers.detail(id)}/periods`; },
                    salaries: id => { return `${this.e$teacher.routers.detail(id)}/salaries`; },
                    attendances: id => { return `${this.e$teacher.routers.detail(id)}/attendances`; },
                },
                lists: false,
                loadTeacherList: parameter => {
                    if( this.e$teacher.lists === false ) {
                        this.$http.get(this.e$teacher.router.index, {params: parameter}).then(res => {
                            if( res.code === 0 ) {
                                this.e$teacher.lists = res.data;
                            }
                        });
                    }
                    return this.e$teacher.lists;
                }
            },
            e$resource: {
                router: {
                    index: 'api/resource'
                },
                lists: false,
                loadResourceList: parameter => {
                    if( this.e$resource.lists === false ) {
                        this.$http.get(this.e$resource.router.index, {params: parameter}).then(res => {
                            if( res.code === 0 ) {
                                this.e$resource.lists = res.data;
                            }
                        });
                    }
                    return this.e$resource.lists;
                }
            },
            e$ware: {
                routers: {
                    index: (sectionID) => { return `api/section/${sectionID}/ware`; },
                    detail: (sectionID, id) => { return `${this.e$ware.routers.index(sectionID)}/${id}`; },
                }
            },
            e$attendance: {
        
                routers: {
                    index: 'api/attendance',
                    section: (sectionID) => { return `api/section/${sectionID}/attendance`; },
                    attend: (sectionID) => { return `api/section/${sectionID}/attend`; },
                    attendByOp: (sectionID, op) => { return `${this.e$attendance.routers.attend(sectionID)}/${op}`; },
                },
        
                //1.正常 2.迟到 3.早退 4.缺席
                statusList: {
                    1: { label: '正常', type: 'green' },
                    2: { label: '迟到', type: 'pink' },
                    3: { label: '早退', type: 'red' },
                    4: { label: '缺席', type: 'orange' },
                },
        
                /**
                 * 获取课时签到信息
                 * @param sectionID
                 * @returns {*}
                 * @private
                 */
                __fetchSectionAttendance: (sectionID) => {
                    return this.$http.get( this.e$attendance.routers.section(sectionID) );
                },
        
                /**
                 * 角色签到
                 * @param sectionID
                 * @param data
                 * @returns {*}
                 * @private
                 */
                __roleAttend: (sectionID, data) => {
                    return this.$http.post(this.e$attendance.routers.attend(sectionID), data);
                },
        
                /**
                 * 操作签到
                 * @param sectionID
                 * @param op
                 * @returns {*}
                 * @private
                 */
                __roleAttendByOp: (sectionID, op) => {
                    return this.$http.post(this.e$attendance.routers.attendByOp(sectionID, op));
                },
            }
        };
    },
    
};


export { GroupMixin, VideoMixin, UserMixin, EduMixin, CommonMixin };